package com.shop.advertisement.service.impl;

import com.shop.advertisement.service.IndexSlideAdService;
import com.shop.dao.LdbzIndexSlideAdMapper;
import com.shop.pojo.LdbzIndexSlideAd;
import com.shop.utils.FastJsonConvert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class IndexSlideAdServiceImpl implements IndexSlideAdService {

    private static final Logger logger = LoggerFactory.getLogger(IndexSlideAdServiceImpl.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    LdbzIndexSlideAdMapper mapper;

    //获取索引推荐信息的键
    @Value("${redisKey.indexSlide.info.key}")
    private String INDEX_SLIDE_INFO_KEY;
    //redis过期时间
    @Value("${redisKey.expire_time}")
    private Integer REDIS_EXPIRE_TIME;

    /**
     * 获取索引轮播广告页面
     *
     * @param entity
     * @param start
     * @param limit
     * @return
     */
    @Override
    public List<LdbzIndexSlideAd> getIndexSlideAdPage(LdbzIndexSlideAd entity, int start, int limit) {
        logger.debug("getIndexSlideAdPage {} ，{} ，{}", entity, start, limit);
        return mapper.selectByEntity(entity, start, limit);
    }

    /**
     * 获取索引轮播广告数量
     *
     * @param entity
     * @return
     */
    @Override
    public long countByEntity(LdbzIndexSlideAd entity) {
        logger.debug("countByEntity {}", entity);
        return mapper.countByEntity(entity);
    }

    /**
     * 根据id获取索引轮播广告
     *
     * @param id
     * @return
     */
    @Override
    public LdbzIndexSlideAd selectByKey(Long id) {
        logger.debug("selectByKey {}", id);
        return mapper.selectByKey(id);
    }

    /**
     * 删除索引轮播广告
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        logger.debug("deleteByKey {}", id);
        return mapper.deleteByKey(id);
    }

    /**
     * 批量删除索引轮播广告
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteByKeys(List<Long> ids) {
        logger.debug("deleteByKeys {}", ids);
        return mapper.deleteByKeys(ids);
    }

    /**
     * 插入索引轮播广告
     *
     * @param entity
     * @return
     */
    @Override
    public int insertByEntity(LdbzIndexSlideAd entity) {
        logger.debug("insertByEntity {}", entity);
        return mapper.insertByEntity(entity);
    }

    /**
     * 更新索引轮播广告
     *
     * @param entity
     * @return
     */
    @Override
    public int updateByKey(LdbzIndexSlideAd entity) {
        logger.debug("updateByKey {}", entity);
        return mapper.updateByKey(entity);
    }

    /**
     * 获取索引轮播广告
     *
     * @return
     */
    @Override
    public List<LdbzIndexSlideAd> getIndexSlideAdByRedis() {
        logger.debug("getIndexSlideAdByRedis");
        try {
//            String json = redisClient.get(INDEX_SLIDE_INFO_KEY);
            String json = String.valueOf(redisTemplate.opsForValue().get(INDEX_SLIDE_INFO_KEY));
            if (StringUtils.isEmpty(json)) {
                logger.info("Redis 获取首页广告");
                return FastJsonConvert.convertJSONToArray(json, LdbzIndexSlideAd.class);
            } else {
                logger.error("Redis 查询不到首页广告");
            }
        } catch (Exception e) {
            logger.error("获取首页广告缓存信息报错", e);
        }
        List<LdbzIndexSlideAd> ret = mapper.selectByEntity(new LdbzIndexSlideAd(), 0, Integer.MAX_VALUE);
        try {
            logger.info("Redis 更新首页广告");
            redisTemplate.opsForValue().set(INDEX_SLIDE_INFO_KEY, FastJsonConvert.convertObjectToJSON(ret));
            redisTemplate.getExpire(INDEX_SLIDE_INFO_KEY);
        } catch (Exception e) {
            logger.error("Redis 更新首页广告缓存信息报错", e);
        }
        return ret;
    }
}
